package com.connector.server.start;

import org.slf4j.LoggerFactory;
import java.util.Iterator;
import com.connector.server.util.PushMsgUtil;
import com.connector.base.exception.ConnectorException;
import com.google.common.base.Strings;
import com.connector.base.connection.impl.SocketIoConnection;
import com.connector.base.connection.impl.NettyConnection;
import com.connector.base.connection.Connection;
import com.connector.base.connection.channel.SocketChannel;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import java.util.concurrent.ConcurrentMap;
import io.netty.util.TimerTask;
import io.netty.util.HashedWheelTimer;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import com.connector.base.connection.impl.LocalConnectionRegistry;
import org.slf4j.Logger;

public class HandshakeCheckTimer
{
    private static final Logger log;

    public HandshakeCheckTimer() {
        final ConcurrentMap<SocketChannel, Connection> connections = LocalConnectionRegistry.getConnections();
        final Timer timer = (Timer)new HashedWheelTimer(Executors.defaultThreadFactory(), 2L, TimeUnit.MINUTES, 1);
        final TimerTask task1 = (TimerTask)new TimerTask() {
            public void run(final Timeout timeout) throws Exception {
                HandshakeCheckTimer.checkHandshake(connections);
                timer.newTimeout((TimerTask)this, 1L, TimeUnit.SECONDS);
            }
        };
        timer.newTimeout(task1, 1L, TimeUnit.SECONDS);
    }

    public static void checkHandshake(final ConcurrentMap<SocketChannel, Connection> connections) {
        if (null != connections) {
            if (!connections.isEmpty()) {
                final long nowTime = System.currentTimeMillis();
                for (final Connection connection : connections.values()) {
                    if (connection instanceof NettyConnection) {
                        continue;
                    }
                    if (!(connection instanceof SocketIoConnection)) {
                        continue;
                    }
                    final SocketIoConnection c = (SocketIoConnection)connection;
                    final String isMobile = c.getSessionContext().getMobile();
                    if (Strings.isNullOrEmpty(isMobile)) {
                        throw new ConnectorException("SocketIoConnection.getSessionContext mobile is null");
                    }
                    if ("2".equals(isMobile)) {
                        continue;
                    }
                    final long time1 = c.getSessionContext().getTime1();
                    final long time2 = c.getSessionContext().getTime2();
                    final String userId = c.getSessionContext().getUserId();
                    if (time2 == 0L) {
                        if (time1 == 0L) {
                            HandshakeCheckTimer.log.debug("userId:{}对比的当前时间 between time:{},{}:between second:{}", new Object[] { userId, time1, nowTime, (nowTime - time1) / 1000L });
                        }
                        else {
                            if (!over2Times(time1, nowTime)) {
                                continue;
                            }
                        }
                    }
                    else {
                        HandshakeCheckTimer.log.debug("userId:{}对比的为session时间 between time:{},{}:between second:{}", new Object[] { userId, time1, time2, (time2 - time1) / 1000L });
                        if (!over2Times(time1, time2)) {
                            continue;
                        }
                    }
                }
            }
        }
    }

    public static boolean over2Times(final Long time1, final Long time2) {
        if (null == time1 || null == time2 || time1 == 0L || time2 == 0L) {
            return false;
        }
        final long t = time2 - time1;
        final double d = (double)(t / 1000L);
        return d >= 120.0;
    }

    static {
        log = LoggerFactory.getLogger((Class)HandshakeCheckTimer.class);
    }
}
